package cn.hjh.service;

import cn.hjh.dao.DeptDao;
import cn.hjh.dao.HibernateUtil;
import cn.hjh.pojo.Dept;
import cn.hjh.pojo.Emp;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;


public class DeptBiz {
	private DeptDao dao = new DeptDao();

	public void addNewEmp(Dept dept) {
		Transaction tx = null;
		try {
			tx = HibernateUtil.currentSession().beginTransaction();
			dao.save(dept);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}

	public void deleteDept(Dept dept){
		Transaction tx = null;
		try {
			tx = HibernateUtil.currentSession().beginTransaction();
			dao.delete(dept);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}

	public void shili15(){
		Transaction tx = null;
		try {
			tx = dao.currentSession().beginTransaction();
//			Dept dept = (Dept) HibernateUtil.currentSession().load(Dept.class,new Integer(10));
			Dept dept = (Dept) HibernateUtil.currentSession().get(Dept.class,new Integer(10));
//			Emp emp = new Emp();
//			emp.setEmpName("Tom");
//			emp.setDept(dept);
//			HibernateUtil.currentSession().save(emp);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}

	public Dept findDeptByDeptNo(Integer deptno) throws HibernateException{
		return this.dao.findById(deptno);
	}
}
